// pages/home/home.js

import { Home } from 'home-model.js';
var utils = require('../../utils/config.js')

var home = new Home(); //实例化 首页 对象
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    src:'../../images/home/q5.png',
    selectArr:[],
    scrollTop:0,
    toplist:[],
    listArr:[],
    adds:[],
    hidden: true,
    flag:true,
    district:'',
    cate_id:0,
    p:1,
    word:''
  },

  bindPickerChange: function (e) {
    var index = e.detail.value;
    var currentId = this.data.selectArr[index].id;
    this.setData({
      cate_id: currentId
    })

    this._getInfoData();
  },
  bindCateChange: function (e) {
    var cate_id = e.target.id;
    this.setData({
      cate_id: cate_id
    })

    this._getInfoData();
  },

  bindrAdressChange: function (e) {
    var index = e.detail.value;
    var addId = this.data.adds[index].id;
    var address = this.data.adds[index].name;
    wx.setStorageSync('district_id', addId);
    wx.setStorageSync('district',address);
    this.setData({
      district: address,
    })

    this._getInfoData();
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   // wx.clearStorage();
   // return false;
    var that = this;
    app.login(function (showLogin){
      that.setData({
        hidden: !showLogin
      })
    });
   

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      cate_id: 0
    })
    this._loadData();
  },

  /*加载所有数据*/
  _loadData: function () {
    var that = this;

   //查询当前地区是否开通
    var flag = wx.getStorageSync('flag');
    if (flag){
      var district = wx.getStorageSync('district');
      that.setData({
        district: district,
        flag: flag
      });

      // 获得分类信息
      home.getCateData((data) => {
        that.setData({
          selectArr: data,
        });
      });

      that._getInfoData();
    }else{
      this._getLocation();
    }
    
  },

  _getLocation:function(){
    var that=this;
    app.location().then(function (data) {
      if (data.status) {
        that.setData({
          district: data.district,
          flag: true,
        })
        wx.setStorageSync('flag', true);
        wx.setStorageSync('district_id', data.info.id);
        wx.setStorageSync('district', data.district);

        // 获得分类信息
        home.getCateData((res) => {
          that.setData({
            selectArr: res,
          });
        });

        that._getInfoData();
      } else {
        wx.setStorageSync('flag', false);
        wx.showModal({
          title: '提示',
          content: '您所在的地区没有开通业务',
          success: function (res) {
          }
        })
      }
    }, function (data) {
      wx.showModal({
        title: '提示',
        content: '您所在的地区没有开通业务',
        success: function (res) {
        }
      })
    })
  },

  
  _getInfoData:function(){
    wx.showLoading({
      title: '加载中',
    })
    var cate_id = this.data.cate_id;
    var district_id = wx.getStorageSync('district_id');
    var that = this;
    // 获得信息
    home.getInfoData(cate_id, district_id,0,(data) => {
      that.setData({
        toplist: data.top,
        listArr: data.list,
        adds: data.adds
      });
      wx.hideLoading();
    });
  },

  bindgetuserinfo:function(e){
    var that = this;
    if (e.detail.userInfo) {
      wx.login({
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        success: function (res) {
          var code = res.code;  //获取code
          app.Baselogin(code, e.detail);
        },
        fail: function () {
          wx.showToast({
            title: "授权登录失败",
            icon: 'none',
            duration: 2000
          })
        }
      }) 
    } else {
      wx.showToast({
        title: "为了您更好的体验,请先同意授权",
        icon: 'none',
        duration: 2000
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var cate_id = this.data.cate_id;
    var district_id = wx.getStorageSync('district_id');
    var p = this.data.p;
    var newp = p + 1;
    var that = this;
    // 获得信息
    home.getInfoData(cate_id, district_id, p, (data) => {
      that.setData({
        toplist: data.top,
        listArr: data.list,
        p: newp,
      });
      wx.stopPullDownRefresh();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  goDetail:function(e){
    // wx.redirectTo({
    wx.navigateTo({
      url: '../detail/detail?id='+e.currentTarget.id
    });
  },
  toTop:function(){
    this.setData({
      scrollTop:0
    });
  },
  search: function(){
    this.setData({
      show:true
    });
  },
  close: function(e){
    if (e.target.id != 'close' && e.target.id != 'search'){
      return;
    }
    if (e.target.id == 'search'){
      wx.showLoading({
        title: '搜索中',
      })
      var that = this;
      var pdata = {
        "cate_id": this.data.cate_id,
        'district_id': wx.getStorageSync('district_id'),
        'p': this.data.p,
        'word': this.data.word
      }
      
      home.getSearch(pdata,(data) => {
        that.setData({
          toplist: data.top,
          listArr: data.list,
        });
        wx.hideLoading()
      });

    }
    this.setData({
      show:false
    })
  },
  input: function (e) {
    var o = {};
    o[e.target.id] = e.detail.value;
    this.setData(o);
  }

})